define(function (require, exports, module) {
    require('authorize');
    require('permission');
    require('global');
    
    var $ = require("jquery");
    var angular = require('angular');
    var util = require('util'); 
    require('angular-interceptor');
    require("angular-ui");
    require("angular-messages");
    var ui = require("ui");

    require('service/my_application/appstoreService');

    angular.module('appBuy.controllers', [])
        .controller('appBuyController', [
            '$scope', 
            'appStoreService',
            function ($scope, service) {
                $scope.count = 1;
                $scope.during = 2;
                $scope.changeDuring = function(delta){
                    if (delta === -1 && $scope.during === 0) {
                        return;
                    }
                    $scope.during += delta;
                }
                $scope.changeCount = function(delta){
                    if (delta === -1 && $scope.count === 0) {
                        return;
                    }
                    $scope.count += delta;
                }
                $scope.packageList = service.getPackageList();
                
                $scope.searchObj = util.getQueryParams(location.search);
                var iconMap = {
                    "zsslc":"big_zxb",
                    "ydxs":"big_gkb",
                    "qdgj":"big_qdgj",
                    "o2oyy":"big_o2oyy",
                    "wlsold":"big_wlsold",
                    "wcb":"big_wcb"
                };
                
                if ($scope.searchObj.package) {
                    for (var i = 0; i < $scope.packageList.length; i++) {
                        var item = $scope.packageList[i];
                        if (item.id === $scope.searchObj.package) {
                            $scope.package = item;
                            break;
                        }                     
                    }
                    
                    if (!$scope.package) {
                        service.getAppList({
                            search: {},
                            success: function(res) {
                                var myAppList = res.data;
                                for (var i = 0; i < myAppList.length; i++) {
                                    var app = myAppList[i];
                                    if (app.unique_name === $scope.searchObj.package) {
                                        var pkg = {
                                            icon: "big_zxb",
                                            priceDesc: "20000元/年/项目",
                                            priceDetail: {
                                                fee: 20000,
                                                unit: "年"
                                            },
                                            p_type: "package"
                                        }
                                        var apps = [];
                                        for (var i = 0; i < app.applications_info.length; i++) {
                                            var info = app.applications_info[i];
                                            apps.push(info.FriendlyName);
                                        }
                                        $scope.package = angular.extend(pkg,{
                                            icon: iconMap[app.unique_name],
                                            id:app.unique_name,
                                            name: app.friendly_name,
                                            apps: apps
                                        });
                                        break;
                                    }
                                }
                            },
                            fail: function(res) {
                                
                            }
                        })
                    }
                }
            }
        ]);

    angular.module('appBuy', [
        'ngUI',
        'ngInterceptor',
        'appStore.services', 
        'appBuy.controllers'
    ]);

    angular.element(document).ready(function(){
        angular.bootstrap(document, ['appBuy']);
    })
});